
function AddSubAccount () {
    // this.namesPath = namesPath
    // this.apisPath = apisPath
    
    this.idData = {
        name_n : 'bem',
        phone : 'gc6',
        save_add : 'alp',
        sq : 'bv_',
        search : 'iwf',
        search_edit : 'hgp',
        country_list : 'bvg',
        country_number : 'bvh'
    }

    this.stringData = {
        save_add : '保存并继续添加',
        addMember : '添加成员',
        select_s : '选择国家和地区',
        search_text : '搜索',
        country : '美国',
        error_1 : '手机已存在于通讯录，无法添加',
        ok : '确定',
        country_number : '+1',
        load_text : '处理中…'
    }

    this.numberData = {
        sleep : 1000
    }

    AddSubAccount.prototype.init = function (namesPath, apisPath) {
        this.namesPath = namesPath
        this.apisPath = apisPath

    }

    AddSubAccount.prototype._readText_ = function (path) {
        let names = null
        if (files.isFile(path)) {
            names = files.read(path).split('\n')
            if (names.length == 0) {
                return null
            }
            return names
        } else {
            print('没有找到%s路径', path)
            return null
        }
    }

    AddSubAccount.prototype._deleteText_ = function (path, str) {
        if (files.isFile(path)) {
            names = files.read(path)
            if (names.split('\n').length >= 1) {
                names = names.replace(str, '')
                //写入文件
                // print('names:%s', names)
                files.write(path.trim(), names)
                return true
            } else {
                print('没有帐号了')
                return false
            }
        } else {
            print('没有找到%s路径', path)
            return false
        }
    }

    AddSubAccount.prototype._getTextCount_ = function (patharr) {
        let sum = 0
        try {
            patharr.forEach(p => {
                print(p)
                if (files.isFile(p)) {
                    let tempFile = files.read(p).split('\n')
                    if (tempFile.length > sum) {
                        sum = tempFile.length
                    }
                } else {
                    print('%s文件夹不存在', p)
                    throw '错误'
                }
            })
        } catch (err) {
            console.error(err)
        }
        return sum

    }

    AddSubAccount.prototype._selectTimeZone_ = function () {
        text(this.stringData.select_s).waitFor()
        print('搜索美国')
        while(!id(this.idData.search).findOne().click());
        sleep(this.numberData.sleep)
        print('输入')
        while(!id(this.idData.search_edit).findOne().setText(this.stringData.country));
        
        sleep(this.numberData.sleep)
        let countryList = id(this.idData.country_list).findOne()
        if (countryList.childCount() == 0) {
            // print('没有找到该国家')
            throw '没有找到该国家'
        }
        //开始点击该国家
        while(!countryList.child(0).click());
    }

    AddSubAccount.prototype.run = function () {
        // let tool = require('../../../mods/tool')
        // t = new tool
        print('run')
        console.show()
        text(this.stringData.addMember).waitFor()
        print('开始添加')
        let sum = this._getTextCount_([this.apisPath])
        if (sum < 1) {
            print('sum < 1')
            return null
        }
        print('sum:%d', sum)
        toastLog('一共读取到'+sum+'条api数据')
        let apis = files.read(this.apisPath)
        let names = files.read(this.namesPath)
        if (!apis || !names) {
            print('没有找到apis names路径')
            return null
        }

        apis.split('\n').forEach((api, index) => {
            print('index:%d', index)
            print('api:%s', api)
            if (!api || api=='') {
                toastLog('没有Api了')
                return false
            }

            //判断重复
            
            let name = names.split('\n')[index].split('-')[1]
            phone = api.split('-')[0]
            id(this.idData.name_n).waitFor()
            id(this.idData.phone).waitFor()
            print('开始第%d次添加', index+1)

            if (phone == null || name == null) {
                print('结束了')
                return false
            }

            name = name.trim()
            phone = phone.trim()
            
            // let phone = api.match(/\d{9,11}/g)

            print('name:%s \n phone:%s', name, phone)

            // while(!id(this.idData.name_n).findOne().click());
            if (name == '' || phone == '') {
                print('name或者Phone为空')
                return false
            }
            print('输入名字：%s', name)
            while(!id(this.idData.name_n).findOne().setText(name));
            print('输入手机：%s', phone)
            while(!id(this.idData.phone).findOne().setText(phone));

            //判断时区
            if (id(this.idData.country_number).findOne().text() != this.stringData.country_number) {
                //选择时区
    
                while(!id(this.idData.sq).findOne().click());
                this._selectTimeZone_()
            }
            
            //保存并添加
            while(!text(this.stringData.save_add).findOne().click());
            // sleep(this.numberData.sleep)
            //判断等待
            text(this.stringData.load_text).waitFor()
            // print('111')
            while(text(this.stringData.load_text).exists());
            sleep(200)
            //判断添加成功
            if (text(this.stringData.error_1).exists()) {
                sleep(200)
                while(!text(this.stringData.ok).findOne().click());
                sleep(this.numberData.sleep)
                return true
            }
            sleep(this.numberData.sleep)
        })
    }
}

module.exports = AddSubAccount